<template>
  <div class="app-container">
    <div>
      <el-button style="margin-left: 50px" @click="toCategoryAdd" icon="el-icon-plus" type="primary" plain>新增</el-button>

    </div>

    <br />
    <el-table
      v-loading="listLoading"
      :data="list"
      show-header
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="typeId" width="95">
        <template slot-scope="scope">
          <!-- {{ scope.$index }} -->
          {{ scope.row.typeId }}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.typeName }}
        </template>
      </el-table-column>



      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <i style="color: #409eff" class="el-icon-edit"></i>
          <el-button @click="edit(scope.row.typeId)" type="text"
            >修改</el-button
          >
          <i style="color: #409eff" class="el-icon-delete"></i>
          <el-button
            @click="open2(scope.row.typeId)"
            slot="reference"
            type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { listTypes } from "@/api/type";
import { deleteType } from "@/api/type";
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      params: {
        pageNum: 1,
        limit: 10,
      },

    };
  },
  created() {
    this.fetchData();
  },
  methods: {


    handleSizeChange(val) {
      console.log(val);
      this.params.limit = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      listTypes(this.params).then((res) => {
        console.log(res);
        this.listLoading = false;
        this.total=res.data.total;
        this.list = res.data.listType;
        console.log(this.list);
      });
      // getList(this.listQuery).then(response => {
      // this.list = response.data.items
      // this.listLoading = false;
      // })
    },
    open2(typeId) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteType(typeId)
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.fetchData();
            })
            .catch((reason) => {
              this.$message({
                type: "error",
                message: resason,
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(typeId) {
      this.$router.push({
        path: "/type/edit",
        query: {
          typeId: typeId,
        },
      });
    },
    toCategoryAdd(){
      this.$router.push('/type/save')
    }
  },
};
</script>

<style  scoped>
.grid-content {
  display: flex;
  align-items: center;
  justify-content: center;
}
.grid {
  font-weight: 600;
  color: #606266;
}
 /deep/ .has-gutter th{
    background-color: #F8F8F9;
}
</style>>

